﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Drawing.Imaging;
using System.Collections;

namespace BaseHelper
{
    public class ToolsHelper
    {

        //静态文件类型判断
        private static Hashtable htFileType;

        public static string ClearHtml(string html)
        {
            string strText = System.Text.RegularExpressions.Regex.Replace(html, "<[^>]+>", "");
            return System.Text.RegularExpressions.Regex.Replace(strText, "&[^;]+;", "");
        }


        public static string[] Split(string txt, string split)
        {
            return txt.Split(new string[] { split }, StringSplitOptions.RemoveEmptyEntries);
        }

        public static string StrToUrl(string txt)
        {
            try
            {
                return Uri.EscapeDataString(txt);
            }
            catch(Exception ex)
            {
                try
                {
                    return System.Web.HttpUtility.UrlEncode(txt);

                }
                catch (Exception ex2)
                {
                
                }
                
            }
            return txt;
            //
        }

        public static string UrlToStr(string url)
        {
            return System.Web.HttpUtility.UrlDecode(url);
        }

        public static string StrToHtml(string str)
        {
            return System.Web.HttpUtility.HtmlDecode(str);
        }


        public static string GB2312ToUnicode(string str)
        {
            byte[] bts = Encoding.Unicode.GetBytes(str);
            string r = "";
            for (int i = 0; i < bts.Length; i += 2) 
                r += "\\u" + bts[i + 1].ToString("x").PadLeft(2, '0') + bts[i].ToString("x").PadLeft(2, '0');
            return r;
        }

        public static string UnicodeToGB2312(string str)
        {

            MatchCollection mc = Regex.Matches(str, @"\\u([\w]{2})([\w]{2})", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            byte[] bts = new byte[2];
            foreach (Match m in mc)
            {
                bts[0] = (byte)int.Parse(m.Groups[2].Value, NumberStyles.HexNumber);
                bts[1] = (byte)int.Parse(m.Groups[1].Value, NumberStyles.HexNumber);
                str = str.Replace(m.Groups[0].Value, Encoding.Unicode.GetString(bts));
            }
            return str;
        }

        public static string BytesToStr(byte[] bytes, string split = "")
        {
            string returnStr = "";
            if (bytes != null)
            {
                for (int i = 0; i < bytes.Length; i++)
                {
                    returnStr += bytes[i].ToString("X2");
                    if (i < (bytes.Length - 1))
                        returnStr += split;
                }
            }
            return returnStr;
        }

        public static System.Drawing.Color ColorHexToRgb(string color)
        {

            try
            {
                if (color.Length == 0)
                {
                    //如果为空
                    return System.Drawing.Color.FromArgb(0, 0, 0);//设为黑色
                }
                else
                {
                    //转换颜色
                    return System.Drawing.Color.FromArgb(System.Int32.Parse(color.Substring(1, 2), System.Globalization.NumberStyles.AllowHexSpecifier), System.Int32.Parse(color.Substring(3, 2), System.Globalization.NumberStyles.AllowHexSpecifier), System.Int32.Parse(color.Substring(5, 2), System.Globalization.NumberStyles.AllowHexSpecifier));
                }
            }
            catch
            {
                //设为黑色
                return System.Drawing.Color.FromArgb(0, 0, 0);
            }
        }


        public static int GetFileType(string Filepath)
        {
            if (htFileType == null)
            {
                htFileType = new Hashtable();
                htFileType.Add(".jpg", 0);
                htFileType.Add(".gif", 0);
                htFileType.Add(".bmp", 0);
                htFileType.Add(".png", 0);
                htFileType.Add(".mp4", 1);
                htFileType.Add(".avi", 1);
                htFileType.Add(".mov", 1);
                htFileType.Add(".dvd", 1);
                htFileType.Add(".asf", 1);
                htFileType.Add(".wmv", 1);
                htFileType.Add(".rm", 1);
                htFileType.Add(".rmvb", 1);
                htFileType.Add(".ts", 1);
                htFileType.Add(".dat", 1);
                htFileType.Add(".mpg", 1);
                htFileType.Add(".mpeg", 1);
                htFileType.Add(".vob", 1);

                htFileType.Add(".rtf", 2);

            }

            FileInfo finfo = new FileInfo(Filepath);
            if (htFileType.ContainsKey(finfo.Extension.ToLower()))
                return (int)htFileType[finfo.Extension.ToLower()];

            return 0;

        }

    }
}
